package com.dtagri.fjwlw.ui;

import java.util.Calendar;
import java.util.List;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.LayoutParams;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.dtagri.fjwlw.AppContext;
import com.dtagri.fjwlw.R;
import com.dtagri.fjwlw.base.BaseActivity;
import com.dtagri.fjwlw.bean.TjAreaMsg;
import com.dtagri.fjwlw.bean.UserOperate;
import com.dtagri.fjwlw.fragment.HistoricalDataFragment;

/**
 * 历史数据
 * 
 * @author wxjn
 * 
 */
public class HistoricalDataActivity extends BaseActivity {

	public List<TjAreaMsg> getTjAreaMsgs() {
		return tjAreaMsgs;
	}

	public void setTjAreaMsgs(List<TjAreaMsg> tjAreaMsgs) {
		this.tjAreaMsgs = tjAreaMsgs;
	}

	protected List<TjAreaMsg> tjAreaMsgs = AppContext.tjAreaMsgs;
	private TextView tvArea;
	private HistoricalDataFragment historicalDataFragment;

	private int toYear;
	private int toMonth;
	private int toDay;
	private int fromYear;
	private int fromMonth;
	private int fromDay;

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub

	}

	// 初始化为今天
	private void initDate() {
		Calendar cal = Calendar.getInstance();
		toYear = cal.get(Calendar.YEAR);
		toMonth = cal.get(Calendar.MONTH);
		toDay = cal.get(Calendar.DATE);
		cal.add(Calendar.MONTH, -6);
		fromYear = cal.get(Calendar.YEAR);
		fromMonth = cal.get(Calendar.MONTH);
		fromDay = cal.get(Calendar.DATE);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initActionBar();
	}

	private void initActionBar() {
		ActionBar mActionBar = getSupportActionBar();
		View view = getLayoutInflater().inflate(
				R.layout.actionbar_custom_historical_data, null);
		tvArea = (TextView) view.findViewById(R.id.tv_actionbar_area);
		tvArea.setText(AppContext.curSmallArea().getSmallName());
		View back = view.findViewById(R.id.btn_back);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}

		});
		mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		mActionBar.setCustomView(view, params);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.historical_data, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		showFilterDialog();
		return super.onOptionsItemSelected(item);
	}

	private void showFilterDialog() {
		final ZFilterDialog filterDialog = new ZFilterDialog(this);
		filterDialog.setPositiveButton("确定",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface d, int which) {
							UserOperate userOperate2 = filterDialog
									.getUserOperate();
							AppContext.saveUserOperateInfo(userOperate2);

							fromDay = filterDialog.getFromDay();
							fromMonth = filterDialog.getFromMonth();
							fromYear = filterDialog.getFromYear();
							toDay = filterDialog.getToDay();
							toMonth = filterDialog.getToMonth();
							toYear = filterDialog.getToYear();
							tvArea.setText(AppContext.curSmallArea()
									.getSmallName());
							historicalDataFragment.initData();
						d.dismiss();
					}
				});
		filterDialog.show();
	}

	@Override
	protected int getLayoutId() {
		return R.layout.layout_simple_framelayout;
	}

	@Override
	public void initView() {
		initDate();
		setActionBarTitle("历史数据");
	}

	@Override
	protected boolean hasBackButton() {
		return true;
	}

	@Override
	protected void init(Bundle savedInstanceState) {
		super.init(savedInstanceState);
		if (savedInstanceState == null) {
			historicalDataFragment = new HistoricalDataFragment(this);
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, historicalDataFragment).commit();
		}
	}

	@Override
	public void initData() {
	}

	// =======================

	public int getToYear() {
		return toYear;
	}

	public int getToMonth() {
		return toMonth;
	}

	public int getToDay() {
		return toDay;
	}

	public int getFromYear() {
		return fromYear;
	}

	public int getFromMonth() {
		return fromMonth;
	}

	public int getFromDay() {
		return fromDay;
	}
}
